package com.sjtu.network.chatanalysis.util

import java.math.BigInteger
import java.security.MessageDigest

object ChatUtil {

    fun hashStringToInt(input: String): Int {
        // Create a MessageDigest instance for SHA-256
        val md = MessageDigest.getInstance("SHA-256")
        // Compute the hash value of the input string
        val hashBytes = md.digest(input.toByteArray())
        // Convert the hash bytes to a BigInteger
        val hashBigInt = BigInteger(1, hashBytes)
        // Convert the BigInteger to an integer
        return hashBigInt.toInt()
    }

    fun mapToRange(input: Int): Int {
        val result = input % 1001
        return if (result < 0) {
            result + 1001
        } else {
            result
        }
    }

    fun getScore(inputString: String) : Int {
        // 定义正则表达式，匹配数字部分
        val regex = Regex("\\d+")
        // 在输入字符串中查找匹配的数字
        val matchResult = regex.find(inputString)
        // 提取匹配到的数字
        val number = matchResult?.value
        // 将提取到的数字转换为整数
        val score = number?.toIntOrNull()

        return score ?: 500
    }
}